/**
 * Created by Administrator on 2017/10/19.
 */
var mongoose = require('mongoose');
const DataModel = require('../models/comment.model');

exports.create = function(req,res,next){
    const dataModel = new DataModel(req.body);
    dataModel.save()
        .then(data=>{
            res.json(data);
        })
}

exports.get = function(req,res,next){
    var id = req.params.id;
    DataModel.findById(id, function (err, data) {
        res.json(data);
    })
}

exports.update = function(req,res,next){
    const id = req.params.id;

    DataModel.findByIdAndUpdate(id, {$set: req.body}, {new:false})
        .then(data=>{
            res.json(data);
        })
}

exports.getnews = function(req,res,next){
    const id = req.params.id;
    var ids = [];
    DataModel.findOne({_id: id},function(err,doc){
        if(doc){
            ids = [doc._id];
            doc.getChildren().then(function(docs){
                for(var i=0;i<docs.length;i++){
                    ids.push(docs[i]._id);
                }
                DataModel.find({ _id : {$in : ids}})
                    .then(data=>{
                        res.json({"msg":"deletes success","status":200});
                    })
            });
        }
    })
}

exports.deletes = function(req,res,next){
    var idarr=req.body.idarr;
    if(idarr.length>0){
        var s=idarr.split(",");
        DataModel.remove({_id:{$in:s}})
            .then(data=> {
                res.json({"msg":"success","status":200});
            })
    }else {
        res.json({"msg":"faild","status":404});
    }
}

exports.remove = function(req,res,next){
    const id = req.params.id;

    DataModel.findByIdAndRemove(id,function(err,data){
        res.json(data);
    })

}

exports.list = function(req,res,next){
    var page = (req.body.page) ? req.body.page : 1;

    var rows = (req.body.rows) ? req.body.rows : 10;

    // console.log(req.body.newsId);
    // res.json(123);
    var queryCondition = {};
    // if(req.body.newsId && req.body.newsId.trim().length>0){
    //     newsId = req.body.newsId;
    //
    //     queryCondition = {
    //         "content" : new RegExp(newsId, 'i')
    //     }
    // }

    if(req.body.articleId && req.body.articleId.trim().length>0){
        articleId = req.body.articleId;

        queryCondition = {
            "articleId":articleId
        }
    }


    if(req.body.content && req.body.content.trim().length>0){
        content = req.body.content;
        queryCondition = {
            "content" : new RegExp(content, 'i')
        }
    }

    if(req.body.title && req.body.title.trim().length>0){
        title = req.body.title;
        queryCondition = {
            "title" : new RegExp(title, 'i')
        }
    }

    if(req.body.articleId && req.body.articleId.trim().length>0){
        articleId = req.body.articleId;
        // Object.assign对象合并
        queryCondition = Object.assign(queryCondition,
            {
                articleId: articleId,
            }
        )
    }

    if(req.body.categoryId && req.body.categoryId.trim().length>0){
        categoryId = req.body.categoryId;
        // Object.assign对象合并
        queryCondition = Object.assign(queryCondition,
            {
                categoryId:categoryId,
            }
        )
    }

    DataModel.paginate(queryCondition, {sort: { _id: -1 }, page: parseInt(page), limit: parseInt(rows) }, function(err, result) {

        result.rows = result.docs;
        delete result.docs;
        res.json(result)
    });
}